package untils

import (
	"github.com/google/uuid"
	capi "github.com/hashicorp/consul/api"
)

type ConsulClient struct {
	client *capi.Client
}

func NewConsulClient(address string) (*ConsulClient, error) {
	config := capi.DefaultConfig()
	config.Address = address
	client, err := capi.NewClient(config)
	if err != nil {
		panic(err)
		return nil, err
	}
	return &ConsulClient{
		client: client,
	}, err
}

type GRPCServer struct {
	Name    string
	Tags    []string
	Port    int
	Address string
}

func (c *ConsulClient) RegisterServer(cfg *GRPCServer) error {
	resp := &capi.AgentServiceRegistration{
		ID:      uuid.NewString(),
		Name:    cfg.Name,
		Tags:    cfg.Tags,
		Port:    cfg.Port,
		Address: cfg.Address,
		Check:   nil,
	}

	return c.client.Agent().ServiceRegister(resp)
}
